import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

let state = {
  productList: [], // 购物车内所有商品
  productSum: 0, // 购物车内商品总数
  productPrice: 0, // 总价
  isCheckAll: true, // 是否全选
  favoriteList: [], // 收藏列表
  buyList: [], // 实际购买产品
  coinList:[], // 每日签到松鼠币
  coinSum: 0, // 松鼠币总数
};
let mutations = {
  addProduct(state, product) {
    let _index = -1;
    state.productList.forEach(function(val, index, data) {
      if (val.name === product.name) {
        _index = index;
      }
    });
    if (_index != -1) {
      state.productList[_index].count += 1;
      state.productList[_index].isChecked = true;
    } else {
      // product.count = 1;
      Vue.set(product, "count", 1);
      Vue.set(product, "isChecked", true);
      if (typeof product.salesPrice == "object") {
        console.log("改变", product.salesPrice.value);
        Vue.set(product, "realPrice", product.salesPrice.value);
      } else {
        Vue.set(product, "realPrice", product.salesPrice);
      }
      state.productList.push(product);
    }
    state.productSum += 1;
    state.productPrice += parseFloat(product.realPrice);
  },
  delProduct(state, product) {
    let _index = -1;
    state.productList.forEach(function(val, index, data) {
      if (val.name === product.name) {
        _index = index;
      }
    });
    if (_index != -1) {
      state.productList[_index].count -= 1;
      if (state.productList[_index].count <= 0) {
        state.productList.splice(_index, 1);
      }
    }
    state.productSum -= 1;
    state.productPrice -= product.realPrice;
    if (state.productSum < 0) {
      state.productSum = 0;
    }
  },
  changeProductChecked(state, product) {
    product.isChecked = !product.isChecked;
    console.log("isChecked", product.isChecked);
    if (product.isChecked) {
      state.productPrice += parseFloat(product.realPrice) * product.count;
    } else {
      state.productPrice -= parseFloat(product.realPrice) * product.count;
    }
  },
  checkAllProduct(state) {
    state.isCheckAll = !state.isCheckAll;
    state.productPrice = 0;
    for (var i = 0; i < state.productList.length; i++) {
      var product = state.productList[i];
      Vue.set(product, "isChecked", state.isCheckAll);
      if (product.isChecked) {
        state.productPrice += parseFloat(product.realPrice) * product.count;
      }
    }
  },
  clearCheckedProduct(state){
    var cnt = 0;
    for (var i = 0; i < state.productList.length; i++) {
      var product = state.productList[i];      
      if (product.isChecked) {
        cnt += product.count;
        state.productList.splice(i, 1);
        i--;   
        console.log("删除啦啦啦")  
      }
    }
    state.productSum -= cnt;
    state.productPrice =0;
  },
  addFavorite(state, product) {
    let _index = -1;
    state.favoriteList.forEach(function(val, index, data) {
      if (val.name === product.name) {
        _index = index;
      }
    });
    if (_index != -1) {
      state.favoriteList.push(product);
    }
  },
  delFavorite(state, product) {
    let _index = -1;
    state.favoriteList.forEach(function(val, index, data) {
      if (val.name === product.name) {
        _index = index;
      }
    });
    if (_index != -1) {
      state.favoriteList.splice(_index, 1);
    }
  },
  changeFavorite(state, product) {
    let _index = -1;
    state.favoriteList.forEach(function(val, index, data) {
      if (val.name == product.name) {
        _index = index;
      }
    });
    if (_index != -1) {
      state.favoriteList.splice(_index, 1);
      console.log("del");
    } else {
      console.log("add");
      state.favoriteList.push(product);
    }
  },
  // 结算
  account(state, productList) {
    console.log("account")
    let products = [];
    products.push(...productList);
    let sumCnt = 0;
    let sumPrice = 0;
    let time = new Date().toLocaleString();
    for (let i = 0; i < products.length; i++) {
      let product = products[i];
      sumCnt += product.count;
      if (typeof product.salesPrice == "object") {
         sumPrice += product.count * parseFloat(product.salesPrice.value);
      } else {
         sumPrice += product.count * parseFloat(product.salesPrice);
      }
    } 
    sumPrice = sumPrice.toFixed(2);   
    state.buyList.push({
      products,
      sumCnt,
      sumPrice,
      time,
      type: "receiving",      
    });
    console.log(state.buyList);    
  },
  // 签到得松鼠币
  addCoin(state, coin, time){
    state.coinList.push({
      coin,
      time,
    })
  },
  clearCoinList(state){
    state.coinList = [];
  }
};

let getters = {
  productSum(state) {
    return state.productSum;
  },
  productPrice(state) {
    return state.productPrice.toFixed(2);
  },
  checkedList(state) {
    let checkedList = [];
    state.productList.forEach(function(val, index, data) {
      if (val.isChecked) {
        checkedList.push(val);
      }
    });
    return checkedList;
  },
  buySum(state){
    let buySum = 0;
    state.productList.forEach(function(val, index, data) {
      if (val.isChecked) {
        buySum += val.count;
      }
    });
    return buySum;
  },
  buyList(state){
    return state.buyList;
  },
  coinSum(state){
    let coinSum = 0;
    state.coinList.forEach(function(val, index, data) {
       coinSum += val.coin;
    });
    return coinSum;
  }
};
export default new Vuex.Store({
  state,
  mutations,
  getters
});
